package org.ipti.br.tag.pdf;

import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import org.ipti.br.tag.entity.Human;
import org.ipti.br.tag.entity.Unity;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.Serializable;
import java.util.List;
import javax.ejb.Stateless;
import org.ipti.br.tag.util.CalendarUtil;

@Stateless
public class StudentPerUnityPdf extends AbstractPdf implements Serializable {

    private Unity unity;
    private Unity loggedUnity;
    
    public void run(Unity selectedUnity, Unity loggedUnity, List<Human> students)
            throws FileNotFoundException, DocumentException, IOException {
        this.unity = selectedUnity;
        this.loggedUnity = loggedUnity;
        
        this.init("StudentsPerUnity - " + selectedUnity.getName());

        if (students != null || !students.isEmpty()) {
            addMetaData();
            addTitle();
            addHead();
            addBody(students);
        }
        
        close();
        export();
    }

    private void addMetaData() {
        this.getDocument().addTitle("Total de Alunos por Turma");
        this.getDocument().addSubject("");
        this.getDocument().addKeywords("Alunos, Turma, TAG, IPTI");
        this.getDocument().addAuthor("Instituto de Pesquisa em Tecnologia e Inovação - IPTI");
        this.getDocument().addCreator("Tecnologia de Apoio a Gestão - TAG");
    }

    private void addTitle() throws DocumentException {
        Paragraph preface = new Paragraph();
        Paragraph title = new Paragraph("Prefeitura de Santa Luzia do Itanhi", super.getCatFont());
        title.setAlignment(Paragraph.ALIGN_CENTER);
        preface.add(title);

        Paragraph subTitle = new Paragraph("Total de Alunos por Turma", super.getSubFont());
        subTitle.setAlignment(Paragraph.ALIGN_CENTER);
        preface.add(subTitle);

        addEmptyLine(preface, 2);
        this.getDocument().add(preface);
    }
   
    private void addHead() throws DocumentException {
        Paragraph head = new Paragraph();
        Paragraph field = new Paragraph("Escola: ",super.getSubFont());
        field.add(new Phrase(loggedUnity.getName(), super.getSubFont()));
        head.add(field);
        
        field = new Paragraph();
        Phrase line = new Phrase("Turma: " + unity.getName());
        field.add(line);
        head.add(field);
        
        field = new Paragraph();
        Phrase lineDate = new Phrase("Data de Ativação: " + CalendarUtil.getFormatedText(unity.getActivated()));
        line.add("                Capacidade: " + unity.getCapacity());
        field.add(lineDate);
        head.add(field);

        addEmptyLine(head, 1);

        head.setFont(super.getTextF());
        this.getDocument().add(head);
    }

    private void addBody(List<Human> students) throws DocumentException {
        float widths[] = {0.35f,0.15f,0.35f,0.15f};
        PdfPTable table = new PdfPTable(widths);
        table.setWidthPercentage(100);
        PdfPCell cellName = new PdfPCell(new Paragraph("Nome", super.getFontTextBold()));
        PdfPCell cellSex = new PdfPCell(new Paragraph("Sexo", super.getFontTextBold()));
        PdfPCell cellParents = new PdfPCell(new Paragraph("Pais", super.getFontTextBold()));
        PdfPCell cellDate = new PdfPCell(new Paragraph("Data de Nascimento", super.getFontTextBold()));
        cellName.setHorizontalAlignment(Element.ALIGN_CENTER);
        cellSex.setHorizontalAlignment(Element.ALIGN_CENTER);
        cellParents.setHorizontalAlignment(Element.ALIGN_CENTER);
        cellDate.setHorizontalAlignment(Element.ALIGN_CENTER);
        
        table.addCell(cellName);
        table.addCell(cellSex);
        table.addCell(cellParents);
        table.addCell(cellDate);

        for (Human student : students) {
            cellName = new PdfPCell(new Paragraph(student.getName(), super.getFontTextNORMAL()));
            cellSex = new PdfPCell(new Paragraph(student.getSex().toString(), super.getFontTextNORMAL()));
            cellSex.setHorizontalAlignment(Element.ALIGN_CENTER);
            
            String parents = ((student.getFather() != null) ? student.getFather().getName() : "") 
                    + ((student.getMother() != null) ? "\n" + student.getMother().getName() : "")
                    + ((student.getSponsor() != null) ? "\n" + student.getSponsor().getName() : "");
 
            
            cellParents = new PdfPCell(new Paragraph(parents, super.getFontTextNORMAL()));
            
            cellDate = new PdfPCell(new Paragraph(CalendarUtil.getFormatedText(student.getBirthday()), super.getFontTextNORMAL()));
            cellDate.setHorizontalAlignment(Element.ALIGN_CENTER);
            
            table.addCell(cellName);
            table.addCell(cellSex);
            table.addCell(cellParents);
            table.addCell(cellDate);
        }
        this.getDocument().add(table);
    }
}